package com.ruoyi.project.mahjong.controller;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.redis.RedisCache;
import com.ruoyi.project.system.domain.SysUser;
import org.apache.ibatis.annotations.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.mahjong.domain.TOutSend;
import com.ruoyi.project.mahjong.service.ITOutSendService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 断电记录Controller
 *
 * @author ruoyi
 * @date 2023-10-19
 */
@RestController
@RequestMapping("/mahjong/send")
public class TOutSendController extends BaseController
{
    @Autowired
    private ITOutSendService tOutSendService;
    @Autowired
    private RedisCache redisCache;
    /**
     * 查询断电记录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(@RequestParam(value = "tableIdName",required = false) String tableIdName)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long shopId = redisCache.getCacheObject(Constants.SHOP_SUF + user.getUserId());
         TOutSend tOutSend = new TOutSend();
         if (ObjectUtil.isNotNull(tableIdName)){
             tOutSend.setTableName(tableIdName);
         }
        tOutSend.setShopId(shopId.toString());
        startPage();
        List<TOutSend> list = tOutSendService.  selectTOutSendAndMemberNameList(tOutSend);
        return getDataTable(list);
    }

    /**
     * 导出断电记录列表
     */
    @PreAuthorize("@ss.hasPermi('mahjong:send:export')")
    @Log(title = "断电记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TOutSend tOutSend)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long shopId = redisCache.getCacheObject(Constants.SHOP_SUF + user.getUserId());
        tOutSend.setShopId(shopId.toString());
        List<TOutSend> list = tOutSendService.selectTOutSendList(tOutSend);
        ExcelUtil<TOutSend> util = new ExcelUtil<TOutSend>(TOutSend.class);
        return util.exportExcel(list, "send");
    }

    /**
     * 获取断电记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('mahjong:send:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {

        return AjaxResult.success(tOutSendService.selectTOutSendById(id));
    }

    /**
     * 新增断电记录
     */
    @PreAuthorize("@ss.hasPermi('mahjong:send:add')")
    @Log(title = "断电记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TOutSend tOutSend)
    {
        return toAjax(tOutSendService.insertTOutSend(tOutSend));
    }

    /**
     * 修改断电记录
     */
    @PreAuthorize("@ss.hasPermi('mahjong:send:edit')")
    @Log(title = "断电记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TOutSend tOutSend)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long shopId = redisCache.getCacheObject(Constants.SHOP_SUF + user.getUserId());
        tOutSend.setShopId(shopId.toString());
        return toAjax(tOutSendService.updateTOutSend(tOutSend));
    }

    /**
     * 删除断电记录
     */
    @PreAuthorize("@ss.hasPermi('mahjong:send:remove')")
    @Log(title = "断电记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tOutSendService.deleteTOutSendByIds(ids));
    }
}
